import type { RouteDefine } from '#/router'
import type { RouteLocationNormalized, RouteRecordName, Router } from 'vue-router'

export interface MatchRoute {
  (route: RouteLocationNormalized): boolean | Promise<boolean>
}
export class RouteGroup {
  constructor(routeGroup: RouteDefine) {
    this.routeGroup = routeGroup
  }
  routeGroup: RouteDefine = []
  added: boolean = false
  match(route: RouteLocationNormalized) {
    return this.routeGroup.some((r) => r.path === route.path)
  }
  addRoute(router: Router, parentName?: RouteRecordName): boolean {
    if (this.added) return false
    if (parentName) {
      this.routeGroup.forEach((route) => router.addRoute(parentName, route))
    } else {
      this.routeGroup.forEach((route) => router.addRoute(route))
    }
    this.added = true
    return true
  }
  removeRoute(router: Router) {
    this.routeGroup.forEach((route) => router.removeRoute(route.name))
    this.added = false
  }
}
